// pages/template/specMask/specMask.js
class Cart {
    page;
    init(that, data, callback = '') {
        this.page = that;
        this.page.setData({
            //是否展示遮罩
            showMask: false,
            //默认选择的规格的索引
            currentSpecIndex: 0,
            //默认所选规格的数量
            specNum: 1,
            //商品信息
            data: data
        }, function() {
            console.log('callback')
            typeof callback == 'function' ? callback() : '';
        });
        this.chooseSpec();
    }
    closeMask() {
        this.page.setData({
            showMask: false
        })
    }
    showMask() {
        this.page.setData({
            showMask: true
        })
    }
    //选择规格
    chooseSpec(e = null) {
        var index = e ? e.currentTarget.dataset.index : this.page.data.currentSpecIndex;
        this.page.data.data.goods_spec[index].number = this.page.data.data.goods_spec[index].number ? this.page.data.data.goods_spec[index].number : this.page.data.specNum;
        var data = this.page.data.data;
        this.page.setData({
            currentSpecIndex: index,
            dataDetail: data
        });
    }
    //规格跟数量确认
    chooseSure() {
		var wholesaleCar = wx.getStorageSync('wholesaleCar') ? wx.getStorageSync('wholesaleCar') : []
        var index = this.page.data.currentSpecIndex;
        var spec_id = this.page.data.data.goods_spec[index].id;
        var spec_num = this.page.data.data.goods_spec[index].number;
        var spec_stock = this.page.data.data.goods_spec[index].spec_stock;

        var specIdArr = [];
		wholesaleCar.forEach((value) => {
            specIdArr.push(value.spec_id);
        });
        //购物车存在该商品
        if (specIdArr.indexOf(spec_id) > -1) {
            console.log('购物车存在该商品', spec_id)
			for (var value in wholesaleCar) {
				if (wholesaleCar[value].spec_id == spec_id) {
                    //总库存小于当前选择的规格数量时
                    if (spec_stock <= spec_num) {
                        wx.showToast({
                            title: '商品库存不足~',
                            icon: 'none'
                        });
                        break;
                    }
					wholesaleCar[value].number = wholesaleCar[value].number + spec_num
                    break;
                }
            }
			console.log('购物车存在该商品~~~', wholesaleCar)
        } else {
			wholesaleCar.unshift({
                spec_id,
                number: spec_num,
                selectStatus: true
            });
        }
        wx.setStorage({
			key: 'wholesaleCar',
			data: wholesaleCar,
            success: function() {
                var numberCount = 0;
				wholesaleCar.forEach((value) => {
                    numberCount = numberCount + value.number
                });
				wx.setStorageSync("wholesaleNum", numberCount);
                wx.setTabBarBadge({
                    index: 3,
                    text: String(numberCount),
                })
            }
        });
        return true;
    }
    // 立即购买
    nowBuy() {
        var that = this;
        var chooseInfo = this.chooseSure();
        if (!chooseInfo) return false;
        wx.switchTab({
            url: '/pages/cartshopp/cartshopp',
        });
    }
    //加入购物车
    addCart() {
        var that = this;
        var chooseInfo = this.chooseSure();
        if (!chooseInfo) return false;
        wx.showToast({
            title: '已加入到购物车~',
            success: function() {
                setTimeout(function() {
                    // that.page.setData({
                    //   showMask: false
                    // })
                }, 1500)
            }
        });
    }
    //数量加，不能超过库存数
    add() {
        var index = this.page.data.currentSpecIndex;
        if (this.page.data.data.goods_spec[index].spec_stock <= this.page.data.data.goods_spec[index].number) {
            wx.showToast({
                title: '商品库存不足~',
                icon: 'none'
            });
            return false;
        }
        this.page.data.data.goods_spec[index].number++;
        var data = this.page.data.data;
        this.page.setData({
            dataDetail: data
        });
    }
    //数量减去不能少于1
    less() {
        var index = this.page.data.currentSpecIndex;
        if (this.page.data.data.goods_spec[index].number <= 1) return false;
        this.page.data.data.goods_spec[index].number--;
        var data = this.page.data.data;
        this.page.setData({
            dataDetail: data
        });
    }
}
export {
    Cart
};